package com.atguigu.gmall.gateway.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsConfigurationSource;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;
import org.springframework.web.server.WebFilter;

/**
 * @author atguigu-mqx
 */
@Configuration
public class CorsConfig {

    //  放入一个bean 到spring 容器中！
    @Bean
    public WebFilter webFilter(){

        //  创建 CorsConfiguration
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        corsConfiguration.addAllowedOrigin("*");    //  允许所有跨域
        corsConfiguration.addAllowedMethod("*");    //  设置允许跨域的方法 get ,put ...
        corsConfiguration.addAllowedHeader("*");    //  允许请求头设置数据
        corsConfiguration.setAllowCredentials(true);    //  允许携带cookie 数据
        //  创建 CorsConfigurationSource
        UrlBasedCorsConfigurationSource corsConfigurationSource = new UrlBasedCorsConfigurationSource();
        //  CorsConfiguration
        corsConfigurationSource.registerCorsConfiguration("/**",corsConfiguration);
        //  返回数据！
        return new CorsWebFilter(corsConfigurationSource);
    }
}
